package com.lwq.service;

import com.lwq.constants.ScheduleConstants;
import com.lwq.dao.TaskMapper;
import com.lwq.util.ScheduleUtils;
import com.lwq.exception.TaskException;
import com.lwq.model.Dto;
import com.lwq.model.SysTask;
import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/16
 */
@Service
public class TaskService {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private TaskMapper mapper;

    public List<SysTask> queryList(Dto dto){
        return dto == null ? mapper.selectList(null) : mapper.selectByMap(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addTask(SysTask task) throws TaskException, SchedulerException {
        task.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = mapper.insert(task);
        if(rows > 0){
            ScheduleUtils.createScheduleJob(scheduler, task);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void changeStatus(SysTask task) throws SchedulerException {
        int rows = 0;
        String status = task.getStatus();
            if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
            rows = resumeTask(task);
        } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status))
        {
            rows = pauseTask(task);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void run(SysTask task) throws SchedulerException {
        Long taskId = task.getId();
        SysTask model = mapper.selectById(taskId);
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(ScheduleConstants.TASK_PROPERTIES, model);
        scheduler.triggerJob(ScheduleUtils.getJobKey(taskId), dataMap);
    }


    /**
     * 恢复任务
     * @param task
     * @return
     * @throws SchedulerException
     */
    @Transactional(rollbackFor = Exception.class)
    public int resumeTask(SysTask task) throws SchedulerException
    {
        Long taskId = task.getId();
        task.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        int rows = mapper.updateById(task);
        if (rows > 0) {
            scheduler.resumeJob(ScheduleUtils.getJobKey(taskId));
        }
        return rows;
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws SchedulerException
     */
    @Transactional( rollbackFor = Exception.class)
    public int deleteTask(Long id) throws SchedulerException
    {
        int rows = mapper.deleteById(id);
        if (rows > 0) {
            scheduler.deleteJob(ScheduleUtils.getJobKey(id));
        }
        return rows;
    }

    /**
     *暂停任务
     * @param task
     * @return
     * @throws SchedulerException
     */
    @Transactional(rollbackFor = Exception.class)
    public int pauseTask(SysTask task) throws SchedulerException
    {
        Long taskId = task.getId();
        task.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = mapper.updateById(task);
        if (rows > 0) {
            scheduler.pauseJob(ScheduleUtils.getJobKey(taskId));
        }
        return rows;
    }



}
